

package com.hazelcast.query.impl;

import com.hazelcast.query.extractor.ValueCollector;
import com.hazelcast.query.impl.getters.MultiResult;

import java.util.ArrayList;
import java.util.List;

public class DefaultValueCollector implements ValueCollector {

    private Object value;
    private List<Object> values;

    public void addObject(Object valueToCollect) {
        if (values != null) {
            values.add(valueToCollect);
        } else
            if (value == null) {
                value = valueToCollect;
            } else {
                values = new ArrayList<>();
                values.add(value);
                values.add(valueToCollect);
                value = null;
            }
    }

    public Object getResult() {
        if (value != null) {
            return value;
        } else
            if (values != null) {
                return new MultiResult<>(values);
            } else {
                return null;
            }
    }

}
